<template>
  <div class="reference-card">
    <!-- 头部 -->
    <div class="card-header" @click="expanded = !expanded">
      <div class="header-left">
        <el-icon class="expand-icon" :class="{ expanded }">
          <CaretRight />
        </el-icon>
        <span class="source-badge">{{ data.source }}</span>
        <el-tag :type="getTypeColor(data.doc_type)" size="small">
          {{ getTypeLabel(data.doc_type) }}
        </el-tag>
      </div>
      <div class="header-right">
        <div class="similarity-score">
          <div class="score-value">{{ (data.similarity * 100).toFixed(0) }}%</div>
          <el-progress
              :percentage="data.similarity * 100"
              :color="getSimilarityColor(data.similarity)"
              :show-text="false"
              class="score-progress"
          />
        </div>
      </div>
    </div>

    <!-- 预览内容 -->
    <div class="card-preview">
      <p class="preview-text">{{ data.content.substring(0, 150) }}...</p>
    </div>

    <!-- 展开的详细内容 -->
    <el-collapse-transition>
      <div v-if="expanded" class="card-expanded">
        <!-- 完整内容 -->
        <div class="expanded-content">
          <p class="full-text">{{ data.content }}</p>
        </div>

        <!-- 元数据 -->
        <div class="expanded-metadata">
          <div class="metadata-row">
            <span class="metadata-label">时间:</span>
            <span class="metadata-value">{{ formatDate(data.created_at) }}</span>
          </div>
          <!-- <div v-if="data.metadata && Object.keys(data.metadata).length > 0" class="metadata-row">
            <span class="metadata-label">标签:</span>
            <span class="metadata-value">
              <el-tag v-for="(value, key) in data.metadata" :key="key" size="small">
                {{ key }}: {{ value }}
              </el-tag>
            </span>
          </div> -->
        </div>

        <!-- 操作按钮 -->
        <div class="card-actions">
          <el-button link type="primary" size="small" @click="copyContent">
            <el-icon><DocumentCopy /></el-icon>
            复制内容
          </el-button>
          <el-button link type="primary" size="small" @click="openDetail">
            <el-icon><View /></el-icon>
            查看原文档
          </el-button>
        </div>
      </div>
    </el-collapse-transition>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { CaretRight, DocumentCopy, View } from '@element-plus/icons-vue'

const props = defineProps({
  data: {
    type: Object,
    required: true,
    validator: (obj) => {
      return obj.source && obj.content && typeof obj.similarity === 'number'
    }
  }
})

const emit = defineEmits(['view-detail'])

const router = useRouter()
const expanded = ref(false)

const typeMap = {
  workorder: '工单',
  fault_analysis: '故障分析',
  equipment_manual: '设备说明书',
  other: '其他'
}

const typeColorMap = {
  workorder: 'info',
  fault_analysis: 'warning',
  equipment_manual: 'success',
  other: 'default'
}

const getTypeLabel = (type) => {
  return typeMap[type] || type
}

const getTypeColor = (type) => {
  return typeColorMap[type] || 'default'
}

const getSimilarityColor = (similarity) => {
  // 使用 Element Plus 的默认颜色值（对应 CSS 变量 --el-color-success/warning/danger）
  if (similarity >= 0.8) return '#67c23a'      // 绿色 - 高相似度 (对应 --el-color-success)
  if (similarity >= 0.5) return '#e6a23c'      // 橙色 - 中相似度 (对应 --el-color-warning)
  return '#f56c6c'                            // 红色 - 低相似度 (对应 --el-color-danger)
}

const formatDate = (date) => {
  if (!date) return '-'
  const d = new Date(date)
  return d.toLocaleDateString('zh-CN')
}

const copyContent = () => {
  navigator.clipboard.writeText(props.data.content).then(() => {
    ElMessage.success('内容已复制到剪贴板')
  }).catch(() => {
    ElMessage.error('复制失败')
  })
}

const openDetail = () => {
  // 先触发事件（保持向后兼容）
  emit('view-detail', props.data)

  // 跳转到知识库页面，并传递文档信息
  try {
    // 使用 source 作为查询参数，在文档列表中定位
    router.push({
      path: '/knowledge-base',
      query: {
        tab: 'list',
        source: props.data.source || '',
        docType: props.data.doc_type || ''
      }
    })
  } catch (err) {
    console.error('跳转失败:', err)
    ElMessage.error('跳转失败，请手动前往知识库页面')
  }
}
</script>

<style scoped>
.reference-card {
  background: var(--el-bg-color);
  border: 1px solid var(--el-border-color-light);
  border-radius: 8px;
  margin-bottom: 12px;
  overflow: hidden;
  transition: all 0.3s ease;
}

.reference-card:hover {
  box-shadow: 0 4px 12px var(--card-shadow-color, rgba(0, 0, 0, 0.1));
  border-color: var(--el-border-color);
}

/* 头部 */
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: var(--el-fill-color-light);
  cursor: pointer;
  transition: background 0.2s ease;
}

.card-header:hover {
  background: var(--el-fill-color);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
}

.expand-icon {
  font-size: 18px;
  color: var(--el-text-color-placeholder);
  transition: transform 0.3s ease;
}

.expand-icon.expanded {
  transform: rotate(90deg);
}

.source-badge {
  font-weight: 600;
  color: var(--el-text-color-primary);
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-left: 15px;
}

/* 相似度评分 */
.similarity-score {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 120px;
}

.score-value {
  font-weight: 700;
  font-size: 16px;
  color: var(--el-color-primary);
  min-width: 35px;
}

.score-progress {
  flex: 1;
  min-width: 60px;
}

:deep(.el-progress__bar) {
  background-image: none;
}

/* 预览内容 */
.card-preview {
  padding: 12px 16px;
  background: var(--el-bg-color);
  border-top: 1px solid var(--el-border-color-lighter);
}

.preview-text {
  margin: 0;
  color: var(--el-text-color-secondary);
  font-size: 13px;
  line-height: 1.5;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  line-clamp: 2;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

/* 展开内容 */
.card-expanded {
  border-top: 1px solid var(--el-border-color-light);
  background: var(--el-fill-color-light);
}

.expanded-content {
  padding: 16px;
  border-bottom: 1px solid var(--el-border-color-light);
}

.full-text {
  margin: 0;
  color: var(--el-text-color-primary);
  font-size: 14px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-wrap: break-word;
  max-height: 300px;
  overflow-y: auto;
}

/* 元数据 */
.expanded-metadata {
  padding: 12px 16px;
  background: var(--el-bg-color);
  border-bottom: 1px solid var(--el-border-color-light);
}

.metadata-row {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 8px;
}

.metadata-row:last-child {
  margin-bottom: 0;
}

.metadata-label {
  font-weight: 600;
  color: var(--el-text-color-secondary);
  min-width: 60px;
  font-size: 12px;
}

.metadata-value {
  flex: 1;
  color: var(--el-text-color-secondary);
  font-size: 12px;
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  align-items: center;
}

/* 操作按钮 */
.card-actions {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  justify-content: flex-end;
}

:deep(.el-button--text) {
  padding: 0;
  height: auto;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
}

@media (max-width: 768px) {
  .card-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .header-right {
    width: 100%;
    margin-left: 0;
    margin-top: 10px;
    justify-content: space-between;
  }

  .similarity-score {
    min-width: auto;
    width: 100%;
  }

  .source-badge {
    max-width: 150px;
  }

  .card-actions {
    flex-wrap: wrap;
  }
}
</style>


















